package com.cy.pj.sys.service.impl;

import com.cy.pj.common.annotation.RequiredLog;
import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;

    @Override
    public void saveObject(SysLog entity) {

        sysLogDao.insertObject(entity);
    }

    @RequiredLog("日志删除")
    @Override
    public int deleteObjects(Integer... ids) {
        if(ids==null||ids.length==0)
            throw new IllegalArgumentException("必须提供正确的id值");
        int rows=sysLogDao.deleteObjects(ids);
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        return rows;
    }

    @RequiredLog("日志查询")
    @Override
    public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent) {
        //1.参数校验
        if(pageCurrent==null|pageCurrent<1)throw new IllegalArgumentException("当前页码不正确");
        //2.查询总记录数并校验
        int rowCount= sysLogDao.getRowCount(username);
        if(rowCount==0)throw new ServiceException("系统没有查到对应的记录");
        //3.查询当前页记录
        int pageSize=5;
        int startIndex=(pageCurrent-1)*pageSize;
        List<SysLog> record=sysLogDao.findPageObjects(username, startIndex, pageSize);
        //4.封装查询结果并返回
        return new PageObject<>(pageCurrent, pageSize, rowCount, record);
    }
}
